package demo4.mapper;

import com.mysql.cj.xdevapi.Result;
import demo4.pojo.Custumer;
import demo4.pojo.User;
import demo4.util.IDButil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.sql.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Component("sun")
public class UserMapper implements IUserMapper{

    @Autowired
    private IDButil dbUtil;

    public int insert(User user) {
        try(Connection connection=dbUtil.getConnection()){
            String sql = "INSERT INTO user(username,password,nickname,age) " +
                    "VALUES(?,?,?,?)";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, user.getUsername());
            ps.setString(2, user.getPassword());
            ps.setString(3, user.getNickname());
            ps.setInt(4, user.getAge());
            return ps.executeUpdate();
        } catch (SQLException e) {
            e.printStackTrace();
        }
//        System.out.println("UserMapper:向user表中插入用户数据.....");
//        Connection connection=dbUtil.getConnection();
//        System.out.println("UserMapper:执行insert操作");
//        System.out.println("UserMapper:执行完毕，返回影响的条目数");
//
        return 0;
    }
    public User login(Custumer custumer){
        try(Connection conn=dbUtil.getConnection();){
            User user=new User();
            String sql="select * from user where username=? and password=?";
            PreparedStatement ps=conn.prepareStatement(sql);
            ps.setString(1,custumer.getUsername());
            ps.setString(2,custumer.getPassword());
            ResultSet rs=ps.executeQuery();
             if(rs.next()){
                 user.setUsername(rs.getString("username"));
                 user.setPassword(rs.getString("password"));
                 user.setNickname(rs.getString("nickname"));
                 return user;
             }
        }catch(Exception e){
            e.printStackTrace();
        }
        return null;
    }
//    public List<User> selectAll(){
//        try (Connection connection = dbUtil.getConnection();){
//            String sql = "SELECT id,username,password,nickname,age " +
//                    "FROM user ";
//            PreparedStatement ps = connection.prepareStatement(sql);
//            ResultSet rs = ps.executeQuery();
//            List<User> list = new ArrayList<>();
//            while(rs.next()){
//                User user=new User();
//                user.setId(rs.getInt("id"));
//                user.setUsername(rs.getString("username"));
//                user.setPassword(rs.getString("password"));
//                user.setNickname(rs.getString("nickname"));
//                user.setAge(rs.getInt("age"));
//                list.add(user);
//            }
//            return list;
//        } catch (SQLException e) {
//            e.printStackTrace();
//            return Collections.emptyList();
//        }
//
//    }
    /**
     * 查询所有用户信息
     *
     * @return 返回所有用户的列表，若查询失败则返回空列表
     */
    public List<User> selectAll() {
        // 使用try-with-resources自动关闭数据库连接
        try (Connection connection = dbUtil.getConnection()) {
            // SQL查询语句，选择用户表中的基本信息
            String sql = "SELECT id,username,password,nickname,age " +
                    "FROM user ";
            // 预编译SQL语句，防止SQL注入
            try (PreparedStatement ps = connection.prepareStatement(sql);
                 ResultSet rs = ps.executeQuery()) {
                // 存储查询结果的用户列表
                List<User> list = new ArrayList<>();
                // 遍历结果集，映射每行数据到User对象
                while (rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setPassword(rs.getString("password"));
                    user.setNickname(rs.getString("nickname"));
                    user.setAge(rs.getInt("age"));
                    list.add(user);
                }
                return list;
            }
        } catch (SQLException e) {
            // 记录错误日志，实际项目中建议使用日志框架
            e.printStackTrace();
            // 返回空列表而非null，避免调用方空指针异常
            return Collections.emptyList();
        }
    }

    public int update(User user) {
        try (Connection connection = dbUtil.getConnection()) {
            String sql1="SELECT username FROM user WHERE username=?";
            try(PreparedStatement ps1 = connection.prepareStatement(sql1)){
                ps1.setString(1, user.getUsername());
                ResultSet rs1 = ps1.executeQuery();
                if(rs1.next()){
                    String sql="UPDATE user SET password=?,nickname=?,age=? WHERE username=?";
                    PreparedStatement ps = connection.prepareStatement(sql);
                    ps.setString(1, user.getPassword());
                    ps.setString(2, user.getNickname());
                    ps.setInt(3, user.getAge());
                    ps.setString(4, user.getUsername());
                    return ps.executeUpdate();
                }
            }
        }catch(SQLException e) {
                e.printStackTrace();
                return 0;
        }
        return 0;
    }




}
